package com.cqc.manage.util;

import com.cqc.manage.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 获取标签 jdbctemplate
 */
@Service
public class JdbcTemplateSql {

    static private Logger LOG = LoggerFactory.getLogger(JdbcTemplateSql.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Map<String, Object>> getAllLabel(){
        List childrenList = new ArrayList();
        String sql = "select * from label_two where label_level_id IN(select label_id from label)";
        String sql_one = "SELECT label.label_id,label.label_name from label";
        List<Map<String, Object>> list_two = jdbcTemplate.queryForList(sql);
        List<Map<String, Object>> list_one = jdbcTemplate.queryForList(sql_one);
        for(Map<String ,Object> map: list_one){
            for (Map<String,Object> map_2: list_two){
                if (map.get("label_id").equals(map_2.get("label_level_id"))) {
                    childrenList.add(map_2);
                }
            }
            map.put("children",childrenList);
            childrenList = new ArrayList();
        }
        LOG.debug("查询结果:{}",list_two);
        LOG.debug("查询结果one:{}",list_one);
        return list_one;
    }

    public List<Map<String, Object>> getUserLabel(User user){
        List childrenList = new ArrayList();
        String sql_two = "select * from label_two where label_level_id IN(" +
                "select label_id from role_label where role_id IN (" +
                "select role_id from user_role WHERE user_id='"+user.getUserId()+"'))";
        String sql_one = "select label_id,label_name from label where label_id in (" +
                "select label_id from role_label where role_id IN (" +
                "select role_id from user_role WHERE user_id='"+user.getUserId()+"'))";
        LOG.debug("查询一级标签的sql:{}",sql_one);
        LOG.debug("查询二级标签的sql:{}",sql_two);
        List<Map<String, Object>> list_two = jdbcTemplate.queryForList(sql_two);
        List<Map<String, Object>> list_one = jdbcTemplate.queryForList(sql_one);
        for(Map<String ,Object> map: list_one){
            for (Map<String,Object> map_2: list_two){
                if (map.get("label_id").equals(map_2.get("label_level_id"))) {
                    childrenList.add(map_2);
                }
            }
            map.put("children",childrenList);
            childrenList = new ArrayList();
        }
        LOG.debug("查询结果:{}",list_two);
        LOG.debug("查询结果one:{}",list_one);
        return list_one;
    }

    /**
     * 通过用户ID查询用户的角色
     * @param userId
     * @return
     */
    public List<String> UserRoleConn(String userId){
        String sql = "select role_id from user_role where user_id = '"+userId+"'";
        LOG.debug("用户角色查询sql:{}",sql);
        List<String> strings = jdbcTemplate.queryForList(sql, String.class);
        LOG.debug("查询结果:{}",strings);
        return strings;
    }

    /**
     * 查询角色权限
     * @param roleId
     * @return
     */
    public List<String> RoleRigthList(String roleId){
        String sql = "select `label_two_name` from `label_two` where label_two_id in (select label_id from role_label where role_id = '"+roleId+"')";
        LOG.debug("查询角色权限:{}",sql);
        List<String> strings = jdbcTemplate.queryForList(sql, String.class);
        LOG.debug("查询结果:{}",strings);
        return strings;
    }



}
